package com.gl.goods.base;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gl.goods.R;


/**
 * Created by hmc on 2018/4/9 0009.
 * Description: 标题栏布局显示以及点击
 */

public class TitleBuilder {
    private View titleView;
    private View view_bottom_line;
    private RelativeLayout titleBar;
    private LinearLayout line_title_bar;
    private TextView text;
    private TextView title_right_update;
    private ImageView leftIco;
    private RelativeLayout ll_finish;
    private ImageView rightIco;
    private ImageView rightSearchIco;
    /**
     * 构造方法：用于获取对象
     * */
    public TitleBuilder(Activity context){
        titleView = context.findViewById(R.id.title_bar_for_main);
        line_title_bar = context.findViewById(R.id.line_title_bar);
        view_bottom_line = context.findViewById(R.id.view_bottom_line);
        text = (TextView)titleView.findViewById(R.id.title_text);
        title_right_update = (TextView)titleView.findViewById(R.id.title_right_update);
        leftIco = (ImageView)titleView.findViewById(R.id.title_leftIco_finish);
        rightIco = (ImageView)titleView.findViewById(R.id.title_rightIco);
        rightSearchIco = (ImageView)titleView.findViewById(R.id.title_rightSearchIcon);
        ll_finish = (RelativeLayout)titleView.findViewById(R.id.ll_title_finish);
    }

    /**
     * 用于设置标题栏文字
     * */
    public TitleBuilder setTitleText(String titleText){
        if(!TextUtils.isEmpty(titleText)){
            text.setText(titleText);
        }
        return this;
    }

    public LinearLayout getLin(){
        return line_title_bar;
    }

    /**
     * 用于设置标题栏左边要显示的图片
     * */
    public TitleBuilder setLeftIco(int resId){
        leftIco.setVisibility(resId > 0 ? View.VISIBLE : View.GONE);
        leftIco.setImageResource(resId);
        return this;
    }

    /**
     * 用于设置标题栏右边保存按钮
     * */
    public TitleBuilder setRightUpdate(String rightText){
        if(!TextUtils.isEmpty(rightText)){
            title_right_update.setText(rightText);
        }
        return this;
    }

    /**
     * 用于设置标题栏右边要显示的图片
     * */
    public TitleBuilder setRightIco(int resId){
        rightIco.setVisibility(resId > 0 ? View.VISIBLE : View.GONE);
        rightIco.setImageResource(resId);
        return this;
    }

    /**
     * 用于设置标题栏右边搜索图片
     * */
    public TitleBuilder setRightSearchIco(int resId){
        rightSearchIco.setVisibility(resId > 0 ? View.VISIBLE : View.GONE);
        rightSearchIco.setImageResource(resId);
        return this;
    }

    public TitleBuilder setBottomLineVis(boolean vis){
        if (vis){
            view_bottom_line.setVisibility(View.GONE);
        }else {
            view_bottom_line.setVisibility(View.VISIBLE);
        }
        return this;
    }
    /**
     * 用于设置标题栏左边图片的单击事件
     * */
    public TitleBuilder setLeftIcoListening(View.OnClickListener listener){
        if(leftIco.getVisibility() == View.VISIBLE){
            ll_finish.setOnClickListener(listener);
        }
        return this;
    }

    /**
     * 用于设置标题栏右边图片的单击事件
     * */
    public TitleBuilder setRightIcoListening(View.OnClickListener listener){
        if(rightIco.getVisibility() == View.VISIBLE){
            rightIco.setOnClickListener(listener);
        }
        return this;
    }

    /**
     * 用于设置标题栏保存点击事件
     * */
    public TitleBuilder setRightUpdateListening(View.OnClickListener listener,boolean vis){
        title_right_update.setVisibility(vis ? View.VISIBLE : View.GONE);
        title_right_update.setOnClickListener(listener);
        return this;
    }

    /**
     * 用于设置标题栏右边搜索图片的单击事件
     * */
    public TitleBuilder setRightSearchIconListening(View.OnClickListener listener){
        if(rightSearchIco.getVisibility() == View.VISIBLE){
            rightSearchIco.setOnClickListener(listener);
        }
        return this;
    }
}
